/*******************************************************************
* ActiveGrade Mobile
* Written by Sean Hickey for ActiveGrade, LLC
* 2011
********************************************************************/


/** ***********************************
	LOGIN/LOGOUT
***************************************/

var baseUrl = '';

function login(root, user, pwd, successfunc, errorfunc) {
	$.ajax({
		type: 'POST',
		url: baseUrl + '/rest/verify/',
		data: '["' + root.replace(/\"/g, '\\"') + '","' + user.replace(/\"/g, '\\"') +'","' + pwd.replace(/\"/g, '\\"') + '"]',
		contentType: 'application/json',
		dataType: 'json',
		success: successfunc,
		error: errorfunc
	});
}

function getRoot() {
	var root = getCookie("orgUrl");
	if (!root) {
		return '/';
	} else {
		return root;
	}
}

function logout() {
	deleteCookie('userID');
	deleteCookie('pwd');
	window.location.href='/';
}

function isLoggedIn() {
	if (getCookie('userID') && getCookie('pwd')) {
		return true;
	}
	else {
		return false;
	}
}

function escapeHtml(text) {
	return $('<div/>').text(text).html();
}

/** ***********************************
	COOKIES
***************************************/
function getCookie(c_name) {
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++)
	{
	  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
	  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
	  x=x.replace(/^\s+|\s+$/g,"");
	  if (x==c_name)
		{
		return unescape(y);
		}
	  }
}

function deleteCookie(name) {
	if ( getCookie(name) ) {
		document.cookie = name + "=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}


/** ***********************************
	ACCOUNT INFORMATION
***************************************/
function getAllCourses(userID, pwd, successfunc, errorfunc) {
	$.ajax({
		url: baseUrl + '/rest/users/' + userID + '/managedcourses',
		dataType: 'json',
		headers: {'X-AG-AUTH': userID + ':' + pwd},
		success: successfunc,
		errorfunc: errorfunc
	});
}

function getCoursesForOrganization(userID, pwd, orgID, successfunc, errorfunc) {
	$.ajax({
		url: baseUrl + '/rest/courses/' + orgID + '/childorgs',
		dataType: 'json',
		headers: {'X-AG-AUTH': userID + ':' + pwd},
		success: successfunc,
		errorfunc: errorfunc
	});
}

function getStudentsForCourse(userID, pwd, courseID, successfunc, errorfunc) {
	$.ajax({
		url: baseUrl + '/rest/courses/' + courseID + '/users/enrolled',
		dataType: 'json',
		headers: {'X-AG-AUTH': userID + ':' + pwd},
		success: successfunc,
		error: errorfunc
	});
}

function getPreviousScores(userId, pwd, studentId, standardId, courseId, successfunc, errorfunc) {
	$.ajax({
		url: baseUrl + '/rest/courses/' + courseId + '/users/' + studentId + '/assessmentscores;standardid=' + standardId,
		dataType: 'json',
		headers: {'X-AG-AUTH': userId + ':' + pwd},
		success: successfunc,
		error: errorfunc
	});
	
}

function getStandardsForCourse(userID, pwd, courseID, successfunc, errorfunc) {
	$.ajax({
		url: baseUrl + '/rest/courses/' + courseID + '/standards/graded',
		dataType: 'json',
		headers: {'X-AG-AUTH': userID + ':' + pwd},
		success: successfunc,
		error: errorfunc
	});
}

function getGradingPolicyForStandard(userID, pwd, policyID, successfunc, errorfunc) {
	if (!policyID) {
		successfunc(null);
	} else {
		$.ajax({
			url: baseUrl + '/rest/standardgradingpolicies;standardgradingpolicyids=%5B' + policyID + '%5D',
			dataType: 'json',
			headers: {'X-AG-AUTH': userID + ':' + pwd},
			success: successfunc,
			error: errorfunc
		});
	}
}

function getMultipleGradingPoliciesForStandard(user, pwd, policyArray, successfunc, errorfunc) {
	var policystr = '%5B';
	for(i = 0; i < policyArray.length; i++) {
		policystr = policystr + policyArray[i];
		if( !(i == (policyArray.length - 1)) ) {
			policystr = policystr + '%2C';
		}
	}
	policystr = policystr + '%5D';
	$.ajax({
		url: baseUrl + '/rest/standardgradingpolicies;standardgradingpolicyids=' + policystr,
		dataType: 'json',
		headers: {'X-AG-AUTH': userID + ':' + pwd},
		success: successfunc,
		error: errorfunc
	});
}

function submitScoreForStandard(userID, pwd, assessmentname, score, studentID, courseID, standardID, comment, successfunc, errorfunc) {
	var currenttime = new Date();
	if (score && score != -1) {
		dataString = '[{"score":' + score + ',"measuredUserId":"' + studentID + '","organizationId":"' + courseID + '","standardId":"' + standardID + '","comment":"' + comment.replace(/\"/g, '\\"') + '","dateMeasured":"' + currenttime.getTime() + '"}]';
	} else {
		dataString = '[{"measuredUserId":"' + studentID + '","organizationId":"' + courseID + '","standardId":"' + standardID + '","comment":"' + comment.replace(/\"/g, '\\"') + '","dateMeasured":"' + currenttime.getTime() + '"}]';
	}
	$.ajax({
		type: 'POST',
		url: baseUrl + '/rest/oneoffassessment?assessmentname=' + encodeURIComponent(assessmentname) + '&gradingperiodid=0',
		contentType: 'application/json',
		dataType: 'json',
		headers: {'X-AG-AUTH': userID + ':' + pwd},
		data: dataString,
		success: successfunc,
		error: errorfunc
	});
}